package com.example.Socket;

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread{
    private ServerSocket serverSocket;
    public GreetingServer(int port) throws IOException{
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(10000);
    }

    public void run(){
        while(true){
            try {
                System.out.println("等待远程连接，端口号为：" + serverSocket.getLocalPort());
                Socket server = serverSocket.accept();  // 侦听并接受到此套接字的连接。
                System.out.println("远程主机地址：" + server.getRemoteSocketAddress());  // 返回此套接字连接的端点的地址，如果未连接则返回 null。
                DataInputStream in = new DataInputStream(server.getInputStream()); //返回此套接字的输入流。
                System.out.println(in.readUTF());
                DataOutputStream out = new DataOutputStream(server.getOutputStream());
                out.writeUTF("谢谢连接我：" + server.getLocalSocketAddress() + "\nGoodbye!");
                server.close();
            } catch (SocketTimeoutException s){
                System.out.println("Socket timed out");
                break;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);

        try {
            Thread t = new GreetingServer(port);
            t.run();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
